const { t } = useI18n()

// 必填项
export const required = {
  required: true,
  message: t('common.required')
}

// 最大值最小值 最大长度最小长度判断
type TLimit = number | null
export const validateLimit = (min: TLimit = null, max: TLimit = null,isrequire:boolean=false) => {
  return (rule: any, value: any, callback: any,source:any, options:any) => {
    // 如何非必填，那么空字符不校验
    if(isrequire === false && (value==='' || value === undefined)){
      callback()
    }

    const type = typeof value
    
    if (type === 'string') {
      if ((min === null || value.length >= min) && (max === null || value.length <= max)) {
        callback()
      } else {
        const minTip = `最小长度: ${min};`
        const maxTip = `最大长度: ${max};`
        callback(new Error(`${min === null ? '' : minTip} ${max === null ? '' : maxTip}`))
      }
    } else if (type === 'number') {
      if ((min === null || value >= min) && (max === null || value <= max)) {
        callback()
      } else {
        const minTip = `最小值: ${min};`
        const maxTip = `最大值: ${max};`
        callback(new Error(`${min === null ? '' : minTip} ${max === null ? '' : maxTip}`))
      }
    } else {
      callback(new Error('未匹配正确类型!'))
    }
  }
}
